package insight;

import java.util.*;

/**
 * @author huu
 * @create 2025-08-25-11:17
 */
public class TVStation {
    //作为总线，将天气站信息抽离

    //添加订阅信息
//    private List<User> users = new ArrayList<>();
//    public void subscribe(User user){
//        users.add(user);
//    }
    private List<EventListener> listenerList = new ArrayList<>();
    private Map<Class<? extends Event>, List<EventListener>> eventListenerMap = new HashMap<>();
    //订阅
    public void subscribe(EventListener listener, Class<? extends Event> eventClass){
        //listenerList.add(listener);
        eventListenerMap.computeIfAbsent(eventClass, k -> new ArrayList<>()).add(listener);
    }

//    public void onInfoUpdate(String info){
//        for (User user : users) {
//            user.reciveinfo(info);
//        }
//    }

    //发布事件
    public void publishEvent(Event event){
        Class<? extends Event> eventClass = event.getClass();
        List<EventListener> listeners = eventListenerMap.get(eventClass);
        if(listeners != null){
            for (EventListener listener : listeners) {
                listener.onEvent(event);
            }
        }
//        for (EventListener listener : listenerList) {
//            listener.onEvent(event);
//        }
    }


}
